// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TypeDescriptorProvider.Enums.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Globalization;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    #endregion

    /// <summary>
    /// The enum type description provider.
    /// </summary>
    public class EnumTypeDescriptionProvider : System.ComponentModel.TypeDescriptionProvider
    {
        #region Public Methods

        /// <summary>
        /// The get type descriptor.
        /// </summary>
        /// <param name="objectType">
        /// The object type.
        /// </param>
        /// <param name="instance">
        /// The instance.
        /// </param>
        /// <returns>
        /// </returns>
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            return new TypeDescriptor(objectType);
        }

        #endregion

        /// <summary>
        /// The converter.
        /// </summary>
        internal class Converter : StringConverter
        {
            #region Constants and Fields

            /// <summary>
            /// The enumerationType.
            /// </summary>
            private readonly Type enumerationType;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="Converter"/> class.
            /// </summary>
            /// <param name="enumerationType">
            /// The enumerationType.
            /// </param>
            public Converter(Type enumerationType)
            {
                this.enumerationType = enumerationType;
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The can convert from.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="sourceType">
            /// The source type.
            /// </param>
            /// <returns>
            /// The can convert from.
            /// </returns>
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                var v = base.CanConvertFrom(context, sourceType);
                return v;
            }

            /// <summary>
            /// The can convert to.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="destinationType">
            /// The destination type.
            /// </param>
            /// <returns>
            /// The can convert to.
            /// </returns>
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                var v = base.CanConvertTo(context, destinationType);
                return v;
            }

            /// <summary>
            /// The convert from.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="culture">
            /// The culture.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <returns>
            /// The convert from.
            /// </returns>
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                var v = base.ConvertFrom(context, culture, value);
                return v;
            }

            /// <summary>
            /// The convert to.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="culture">
            /// The culture.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <param name="destinationType">
            /// The destination type.
            /// </param>
            /// <returns>
            /// The convert to.
            /// </returns>
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                var representation = Constants.NotSet;
                if ((int)value != 0)
                {
                    representation = value.ToString();
                }

                var f = this.enumerationType.GetField(value.ToString());
                if (f != null)
                {
                    // If [EnumDisplay(string s)] is present, replace value.ToString() with s value
                    var displayAttributes = f.GetCustomAttributes(typeof(EnumDisplayAttribute), true);
                    if (displayAttributes.Length > 0)
                    {
                        representation = (displayAttributes[0] as EnumDisplayAttribute).Text;
                    }

                    // Add the description in parenthesis if any
                    var descriptionAttributes = f.GetCustomAttributes(typeof(EnumDescriptionAttribute), true);
                    if (descriptionAttributes.Length > 0)
                    {
                        representation = string.Format("{0} ({1})", representation, (descriptionAttributes[0] as EnumDescriptionAttribute).Text);
                    }
                }

                return representation;
            }

            /// <summary>
            /// The get standard values.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <returns>
            /// </returns>
            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                var v = base.GetStandardValues(context);
                return v;
            }

            /// <summary>
            /// The get standard values exclusive.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <returns>
            /// The get standard values exclusive.
            /// </returns>
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                var v = base.GetStandardValuesExclusive(context);
                return v;
            }

            /// <summary>
            /// The get standard values supported.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <returns>
            /// The get standard values supported.
            /// </returns>
            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                var v = base.GetStandardValuesSupported(context);
                return v;
            }

            #endregion
        }

        /// <summary>
        /// The editor.
        /// </summary>
        internal class Editor : UITypeEditor
        {
            #region Constants and Fields

            /// <summary>
            /// The enumerationType.
            /// </summary>
            private readonly Type enumerationType;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="Editor"/> class.
            /// </summary>
            /// <param name="enumerationType">
            /// The enumerationType.
            /// </param>
            public Editor(Type enumerationType)
            {
                this.enumerationType = enumerationType;
            }

            #endregion

            // Displays the UI for value selection.
            #region Public Methods

            /// <summary>
            /// The edit value.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="provider">
            /// The provider.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <returns>
            /// The edit value.
            /// </returns>
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    var listBox = new ListBox();
                    listBox.BorderStyle = BorderStyle.None;
                    listBox.SelectedIndexChanged += (object sender, EventArgs args) =>
                        {
                            var t = listBox.SelectedItem as Item;
                            value = Enum.Parse(this.enumerationType, t.ValueText);
                            editorService.CloseDropDown();
                        };
                    var fieldsInfo = this.enumerationType.GetFields();
                    foreach (var f in fieldsInfo)
                    {
                        if (f.Name != "value__")
                        {
                            var item = new Item(f.Name);
                            item.Instance = f;

                            var displayAttributes = f.GetCustomAttributes(typeof(EnumDisplayAttribute), true);
                            if (displayAttributes.Length > 0)
                            {
                                item.DisplayText = (displayAttributes[0] as EnumDisplayAttribute).Text;
                            }

                            var descriptionAttributes = f.GetCustomAttributes(typeof(EnumDescriptionAttribute), true);
                            if (descriptionAttributes.Length > 0)
                            {
                                item.DescriptionText = (descriptionAttributes[0] as EnumDescriptionAttribute).Text;
                            }

                            listBox.Items.Add(item);
                        }
                    }

                    editorService.DropDownControl(listBox);
                }

                return value;
            }

            /// <summary>
            /// The get edit style.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <returns>
            /// </returns>
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }

            #endregion

            /// <summary>
            /// The item.
            /// </summary>
            internal class Item
            {
                #region Constructors and Destructors

                /// <summary>
                /// Initializes a new instance of the <see cref="Item"/> class.
                /// </summary>
                /// <param name="name">
                /// The name.
                /// </param>
                public Item(string name)
                {
                    this.ValueText = name;
                }

                #endregion

                #region Properties

                /// <summary>
                /// Gets or sets DescriptionText.
                /// </summary>
                public string DescriptionText { get; set; }

                /// <summary>
                /// Gets or sets DisplayText.
                /// </summary>
                public string DisplayText { get; set; }

                /// <summary>
                /// Gets or sets Instance.
                /// </summary>
                public object Instance { get; set; }

                /// <summary>
                /// Gets or sets ValueText.
                /// </summary>
                public string ValueText { get; set; }

                #endregion

                #region Public Methods

                /// <summary>
                /// The to string.
                /// </summary>
                /// <returns>
                /// The to string.
                /// </returns>
                public override string ToString()
                {
                    string value;
                    if (this.DisplayText != null)
                    {
                        value = this.DisplayText;
                    }
                    else
                    {
                        value = this.ValueText;
                    }

                    if (this.DescriptionText != null)
                    {
                        value = value + " (" + this.DescriptionText + ")";
                    }

                    return value;
                }

                #endregion
            }
        }

        /// <summary>
        /// The type descriptor.
        /// </summary>
        internal class TypeDescriptor : CustomTypeDescriptor
        {
            #region Constants and Fields

            /// <summary>
            /// The enumerationType.
            /// </summary>
            private readonly Type enumerationType;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="TypeDescriptor"/> class.
            /// </summary>
            /// <param name="enumerationType">
            /// The enumerationType.
            /// </param>
            public TypeDescriptor(Type enumerationType)
            {
                this.enumerationType = enumerationType;
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The get converter.
            /// </summary>
            /// <returns>
            /// </returns>
            public override TypeConverter GetConverter()
            {
                return new Converter(this.enumerationType);
            }

            /// <summary>
            /// The get editor.
            /// </summary>
            /// <param name="editorBaseType">
            /// The editor base type.
            /// </param>
            /// <returns>
            /// The get editor.
            /// </returns>
            public override object GetEditor(Type editorBaseType)
            {
                return new Editor(this.enumerationType);
            }

            #endregion
        }
    }
}